import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.BucketLoggingResult;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.SetBucketLoggingRequest;
import com.shop.Product_1001;
import com.shop.mapper.Pms_Category_Mapper;
import com.shop.service.Pms_Category_Service;
import com.shop.utils.RedisUtil;
import com.shop.vo.CatalogByTree;
import com.shop.vo.OssUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@SpringBootTest(classes = Product_1001.class)
@RunWith(SpringRunner.class)
public class test1 {
    @Autowired
    private Pms_Category_Mapper pms_category_mapper;
    @Autowired
    private Pms_Category_Service pms_category_service;
    @Autowired
    private RedisUtil redisUtil;


    @Test
    public void test1() {
        List<CatalogByTree> tree = pms_category_mapper.getTree();
        tree.forEach(System.out::println);

    }

    @Test
    public void test2() {
        List<CatalogByTree> tree = pms_category_service.getTree();
        tree.forEach(System.out::println);
    }

    @Test
    public void test3() {


        Object test = redisUtil.get("test");
        if (redisUtil.hasKey("test")) {
            System.out.println("存在");
        } else {
            redisUtil.set("test", "我是测试");
            redisUtil.expire("test", 3600000);
            System.out.println(test);
        }
    }

    //上传图片
    @Test
    public void fileImg() throws FileNotFoundException {
        // Endpoint以杭州为例，其它Region请按实际情况填写。
        String endpoint = "oss-cn-beijing.aliyuncs.com";
        // 云账号AccessKey有所有API访问权限，建议遵循阿里云安全最佳实践，创建并使用RAM子账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建。
        String accessKeyId = "LTAI4GKFAYpfwW3is2KsrKxm";
        String accessKeySecret = "c4IxijRBb5sIETK9fAkzHiSK3F2CTo";

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // 上传文件流。
        InputStream inputStream = new FileInputStream("E:\\Storage\\大二下\\图片\\6.jpg");
        ossClient.putObject("websites-images", "6.jpg", inputStream);

        System.out.println("上傳成功...");
        // 关闭OSSClient。
        ossClient.shutdown();
    }

    //单个删除图片

    public Boolean delImg(){
        // Endpoint以杭州为例，其它Region请按实际情况填写。
        String endpoint = "oss-cn-beijing.aliyuncs.com";
        // 云账号AccessKey有所有API访问权限，建议遵循阿里云安全最佳实践，创建并使用RAM子账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建。
        String accessKeyId = "LTAI4GKFAYpfwW3is2KsrKxm";
        String accessKeySecret = "c4IxijRBb5sIETK9fAkzHiSK3F2CTo";

        String bucketName = "websites-images";
       String objectName = "https://websites-images.oss-cn-beijing.aliyuncs.com/web-images/dce38fa3-6092-404f-acd4-c14f203c4c1e-1583485434489.jpeg";
       String obj = objectName.substring(52);
        System.out.println(objectName.substring(52));
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        boolean found = ossClient.doesObjectExist("websites-images", objectName);
        System.out.println(found);
        boolean flag = true;
        if(found){
            try {
                //BUCKET所在的存储
                // 删除文件。如需删除文件夹，请将ObjectName设置为对应的文件夹名称。如果文件夹非空，则需要将文件夹下的所有object删除后才能删除该文件夹。
                ossClient.deleteObject(bucketName, objectName);
                flag = true;
            }catch (Exception e){
                flag = false;
            }

        }else {
            flag = false;
        }
        // 关闭OSSClient。
        ossClient.shutdown();

        return flag;

    }


    @Test
    public void delSuccess(){
        // Endpoint以杭州为例，其它Region请按实际情况填写。
        String endpoint = "oss-cn-beijing.aliyuncs.com";
        // 云账号AccessKey有所有API访问权限，建议遵循阿里云安全最佳实践，创建并使用RAM子账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建。
        String accessKeyId = "LTAI4GKFAYpfwW3is2KsrKxm";
        String accessKeySecret = "c4IxijRBb5sIETK9fAkzHiSK3F2CTo";

        String bucketName = "websites-images";
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
//        SetBucketLoggingRequest request = new SetBucketLoggingRequest("websites-images");
//        // 设置存放日志文件的存储空间。
//        request.setTargetBucket("websites-images");
//        // 设置日志文件存放的目录。
//        request.setTargetPrefix("oss-accesslog/");
//        ossClient.setBucketLogging(request);



        String objectName = "https://websites-images.oss-cn-beijing.aliyuncs.com/web-images/3f92f855-62f4-46e3-9202-bd22680b9569-6.jpg";
        String obj = objectName.substring(52);
        System.out.println(objectName.substring(52));

        // 判断文件是否存在。doesObjectExist还有一个参数isOnlyInOSS，如果为true则忽略302重定向或镜像；如果为false，则考虑302重定向或镜像。
        boolean found = ossClient.doesObjectExist("websites-images", obj);
        System.out.println(found);


        // 删除文件。如需删除文件夹，请将ObjectName设置为对应的文件夹名称。如果文件夹非空，则需要将文件夹下的所有object删除后才能删除该文件夹。
        ossClient.deleteObject(bucketName, obj);
        System.out.println("删除");


//        BucketLoggingResult result = ossClient.getBucketLogging("websites-images");
//        System.out.println(result.getTargetBucket());
//        System.out.println(result.getTargetPrefix());
//        System.out.println(result.toString());



        // 关闭OSSClient。
        ossClient.shutdown();
    }


    @Test
    public void testlong(){
        Date date=new Date();
        date.setTime(10000);
        System.out.println(date.toString());
    }
    @Test
    public void testDate(){
        LocalDate now=LocalDate.now();//当前时间
        LocalDate threeForDays = now.plusDays(2);//当前时间开始延后三天

        System.out.println("当前时间："+now);
        System.out.println("三天后:"+threeForDays);

        LocalTime min=LocalTime.MIN;
        LocalTime max=LocalTime.MAX;
        System.out.println("min："+min);
        System.out.println("max:"+max);
        LocalDateTime start=LocalDateTime.of(now,min);

        LocalDateTime end=LocalDateTime.of(threeForDays,max);
        String startstr = start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        String endstr = end.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println("当前时间："+startstr);
        System.out.println("三天后:"+endstr);
    }
}
